/**
 * 房间音频配置 API 接口
 */

const baseURL = 'http://localhost:8000/api/room_audio_config'

// 获取房间音频配置
export function getRoomConfigs(roomId) {
  return new Promise((resolve, reject) => {
    console.log('🌐 发起网络请求 getRoomConfigs，房间ID:', roomId);
    uni.request({
      url: `${baseURL}/getRoomConfigs`,
      method: 'GET',
      data: {
        room_id: roomId
      },
      success: (res) => {
        console.log('📡 getRoomConfigs 请求成功，响应:', res);
        if (res.data.code === 1) {
          console.log('✅ getRoomConfigs 数据解析成功:', res.data.data);
          resolve(res.data.data)
        } else {
          console.error('❌ getRoomConfigs 业务失败:', res.data.msg);
          reject(res.data.msg)
        }
      },
      fail: (err) => {
        console.error('💥 getRoomConfigs 网络请求失败:', err);
        reject(err)
      }
    })
  })
}

// 更新房间音频配置
export function updateRoomConfig(data) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: `${baseURL}/updateRoomConfig`,
      method: 'POST',
      data: data,
      header: {
        'Content-Type': 'application/json'
      },
      success: (res) => {
        if (res.data.code === 1) {
          resolve(res.data)
        } else {
          reject(res.data.msg)
        }
      },
      fail: (err) => {
        reject(err)
      }
    })
  })
}

// 获取房间音频列表
export function getRoomAudios(roomId) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: `${baseURL}/getRoomAudios`,
      method: 'GET',
      data: {
        room_id: roomId
      },
      success: (res) => {
        if (res.data.code === 1) {
          resolve(res.data.data)
        } else {
          reject(res.data.msg)
        }
      },
      fail: (err) => {
        reject(err)
      }
    })
  })
}

// 添加音频到房间
export function addAudioToRoom(data) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: `${baseURL}/addAudioToRoom`,
      method: 'POST',
      data: data,
      header: {
        'Content-Type': 'application/json'
      },
      success: (res) => {
        if (res.data.code === 1) {
          resolve(res.data.data)
        } else {
          reject(res.data.msg)
        }
      },
      fail: (err) => {
        reject(err)
      }
    })
  })
}

// 从房间移除音频
export function removeAudioFromRoom(relationId) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: `${baseURL}/removeAudioFromRoom`,
      method: 'POST',
      data: {
        relation_id: relationId
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: (res) => {
        if (res.data.code === 1) {
          resolve(res.data)
        } else {
          reject(res.data.msg)
        }
      },
      fail: (err) => {
        reject(err)
      }
    })
  })
}

// 批量添加音频到房间
export function addMultipleAudiosToRoom(data) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: `${baseURL}/addMultipleAudiosToRoom`,
      method: 'POST',
      data: data,
      header: {
        'Content-Type': 'application/json'
      },
      success: (res) => {
        if (res.data.code === 1) {
          resolve(res.data)
        } else {
          reject(res.data.msg)
        }
      },
      fail: (err) => {
        reject(err)
      }
    })
  })
}

// 更新音频排序
export function updateAudioSort(data) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: `${baseURL}/updateAudioSort`,
      method: 'POST',
      data: data,
      header: {
        'Content-Type': 'application/json'
      },
      success: (res) => {
        if (res.data.code === 1) {
          resolve(res.data)
        } else {
          reject(res.data.msg)
        }
      },
      fail: (err) => {
        reject(err)
      }
    })
  })
}

// 创建房间分类
export function createRoomCategory(data) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: `${baseURL}/createRoomCategory`,
      method: 'POST',
      data: data,
      header: {
        'Content-Type': 'application/json'
      },
      success: (res) => {
        if (res.data.code === 1) {
          resolve(res.data)
        } else {
          reject(res.data.msg)
        }
      },
      fail: (err) => {
        reject(err)
      }
    })
  })
}

/**
 * 删除房间分类
 * @param {string} roomId - 房间ID  
 * @param {string} categoryType - 分类类型
 * @param {string} audioAction - 音频处理方式: 'move'(移动到其他音频) 或 'delete'(删除音频)
 * @returns {Promise}
 */
export function deleteRoomCategory(roomId, categoryType, audioAction = 'move') {
  return new Promise((resolve, reject) => {
    console.log(`调用删除分类API: 房间ID=${roomId}, 分类=${categoryType}, 音频操作=${audioAction}`)
    
    uni.request({
      url: `${baseURL}/deleteRoomCategory`,
      method: 'POST',
      data: {
        room_id: roomId,
        category_type: categoryType,
        audio_action: audioAction
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: (res) => {
        console.log('删除分类API响应:', res.data)
        if (res.data.code === 200) {
          resolve(res.data)
        } else {
          reject(res.data.message || '删除分类失败')
        }
      },
      fail: (err) => {
        console.error('删除分类API请求失败:', err)
        reject(err)
      }
    })
  })
}

/**
 * 更新音频音量
 * @param {number} relationId - 关系ID
 * @param {number} volume - 音量值(0-1)
 * @returns {Promise}
 */
export function updateAudioVolume(relationId, volume) {
  return new Promise((resolve, reject) => {
    console.log(`🔊 调用更新音频音量API: 关系ID=${relationId}, 音量=${volume}`)
    
    uni.request({
      url: `${baseURL}/updateAudioVolume`,
      method: 'POST',
      data: {
        relation_id: relationId,
        volume: volume
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: (res) => {
        console.log('🔊 更新音频音量API响应:', res.data)
        if (res.data.code === 1) {
          resolve(res.data)
        } else {
          reject(res.data.msg || '更新音频音量失败')
        }
      },
      fail: (err) => {
        console.error('🔊 更新音频音量API请求失败:', err)
        reject(err)
      }
    })
  })
}

/**
 * 批量更新音频音量
 * @param {Array} volumeData - 音量数据数组 [{relation_id: 1, volume: 0.8}, ...]
 * @returns {Promise}
 */
export function updateMultipleAudioVolumes(volumeData) {
  return new Promise((resolve, reject) => {
    console.log(`🔊 调用批量更新音频音量API:`, volumeData)
    
    uni.request({
      url: `${baseURL}/updateMultipleAudioVolumes`,
      method: 'POST',
      data: {
        volume_data: volumeData
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: (res) => {
        console.log('🔊 批量更新音频音量API响应:', res.data)
        if (res.data.code === 1) {
          resolve(res.data)
        } else {
          reject(res.data.msg || '批量更新音频音量失败')
        }
      },
      fail: (err) => {
        console.error('🔊 批量更新音频音量API请求失败:', err)
        reject(err)
      }
    })
  })
}